/*
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <eina-0/Eina.h>
#include <Ecore.h>
#include <Ecore_Evas.h>
#include <Edje.h>
#include <Evas.h>
#include <etk/Etk.h>
#include <comic.h>
#include <comic_scrolling.h>
#include <comic_page_menu.h>
#include <string.h>
#include <debug.h>

#define MAX_BUF 1024

Evas *evas;
Ecore_Evas *ee;
struct comic_reader_guis *comic = NULL;

int comic_efl_init(int args, char **argv) {
	if (!ecore_init()) {
		error("failed to init ecore\n");
		return -1;
	}
	if (!ecore_evas_init()) {
		error("failed to init ecore_evas\n");
		return -1;
	}
	if (!edje_init()) {
		error("failed to init edje\n");
		return -1;
	}
	if (!etk_init(args, argv)) {
		error("failed to init etk\n");
		return -1;
	}

	ecore_app_args_set(args, (const char **) argv);
	ee = ecore_evas_software_x11_16_new(NULL, 0,  0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT);
	if (!ee) {
		ee = ecore_evas_software_x11_new(NULL, 0,  0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT);
	}
	if (!ee) {
		error("Cannot get Ecore_Evas. Please check DISPLAY\n");
		return 1;
	}
	ecore_evas_borderless_set(ee, 0);
	ecore_evas_title_set(ee, PACKAGE_NAME);
	ecore_evas_show(ee);
	evas = ecore_evas_get(ee);
	evas_image_cache_set(evas, (4*1024));  // A experience magic
	evas_font_path_append(evas, "fonts/");

	return 1;
}

void set_up_background() {
	Evas_Object *swallow;
	int x, y, w, h;
	evas_object_resize(comic->background, WIDTH(), HEIGHT());
	evas_object_move(comic->background, 0, 0);
	evas_object_show(comic->background);

	if (comic->eventsenser) {
		swallow = (Evas_Object *) edje_object_part_object_get(comic->background, "viewport");
		evas_object_geometry_get(swallow, &x, &y, &w, &h);
		evas_object_move(comic->eventsenser, x, y);
		evas_object_resize(comic->eventsenser, w, h);
		evas_object_move(comic->viewport, x, y);
		evas_object_resize(comic->viewport, w, h);
	}
	if (comic && comic->book && comic->book->current_page) {
		comic_book_goto_index(comic->book, comic->book->index);
		evas_object_move(comic->book->zoom, 0, (100 - comic->book->scale)*(VHEIGHT() - ZOOM_SIZE) / 100);
		evas_object_raise(comic->book->zoom);
	}
}

/*
static void turn_left(void *data,  Evas_Object *obj, const char *emission, const char *source) {
	struct comic_reader_guis *comic = (struct comic_reader_guis *)data;
	if (!data) {
		error("data is null!!\n");
		return;
	}
	if (comic->config->reading_seq == RIGHT_TO_LEFT) {
		comic_book_move_to_next(comic->book);
	} else {
		comic_book_move_to_last(comic->book);
	}
}

static void turn_right(void *data,  Evas_Object *obj, const char *emission, const char *source) {
	struct comic_reader_guis *comic = (struct comic_reader_guis *)data;
	if (!data) {
		error("data is null!!\n");
		return;
	}
	if (comic->config->reading_seq == RIGHT_TO_LEFT) {
		comic_book_move_to_last(comic->book);
	} else {
		comic_book_move_to_next(comic->book);
	}
}
*/

static void show_page_menu(void *data,  Evas_Object *obj, const char *emission, const char *source) {
	static struct Page_Menu *menu = NULL;
	struct comic_reader_guis * comic = (struct comic_reader_guis *)data;
	struct comic_book *book = comic->book;
	if (!data) {
		error("data is null!!\n");
		return;
	}
	menu = page_menu_new (book);
	if (!menu)  {
		debug("No Menu created.!!\n");
		return;
	}
	animator_move_object(etk_embed_object_get(ETK_EMBED(menu->embed)),
	                     &animator_square_move_cb,
	                     ANIMATOR_MAGIC,
	                     0, -HEIGHT(),
	                     0, 0);
}

static void show_book_menu(void *data,  Evas_Object *obj, const char *emission, const char *source) {
	static struct Book_Menu *menu = NULL;
	struct comic_reader_guis *comic = (struct comic_reader_guis *)data;
	char buf[1024];
	Evas_List *list = comic->list;
	debug("\n");
	if (!data) {
		error("data is null!!\n");
		return;
	}
	if (menu) {

		snprintf(buf, sizeof(buf),"<b><font_size=24>%s</><b>", 
				comic->book ? 
				rindex(comic->book->name,'/') ?  rindex(comic->book->name,'/')+1 : comic->book->name : 
				"No Book");
		etk_button_label_set(ETK_BUTTON(menu->label), buf);
		evas_object_move(etk_embed_object_get(ETK_EMBED(menu->embed)), 0, -HEIGHT());
		evas_object_resize(etk_embed_object_get(ETK_EMBED(menu->embed)),WIDTH(), HEIGHT());
		etk_widget_show_all(menu->embed);
		while (evas_object_top_get(evas) != etk_embed_object_get(ETK_EMBED(menu->embed))) {
			debug("raise menu!!\n");
			evas_object_raise(etk_embed_object_get(ETK_EMBED(menu->embed)));
		}
	} else {
		menu = book_menu_list_new (list);
	}
	animator_move_object(etk_embed_object_get(ETK_EMBED(menu->embed)),
	                     &animator_square_move_cb,
	                     ANIMATOR_MAGIC,
	                     0, -HEIGHT(),
	                     0, 0);
}


void setup_callback_functions(struct comic_reader_guis *comic) {
	comic->eventsenser = comic_edje_object_get("comic/eventsenser");
	comic->viewport = comic_edje_object_get("comic/viewport");
	evas_object_event_callback_add(comic->eventsenser,
	                               EVAS_CALLBACK_MOUSE_DOWN,
	                               &on_comic_mouse_cb_dummy,
	                               comic);
	evas_object_event_callback_add(comic->eventsenser,
	                               EVAS_CALLBACK_MOUSE_DOWN,
	                               &on_comic_mouse_cb_down,
	                               comic);
	evas_object_event_callback_add(comic->eventsenser,
	                               EVAS_CALLBACK_MOUSE_UP,
	                               &on_comic_mouse_cb_up,
	                               comic);
	evas_object_event_callback_add(comic->eventsenser,
	                               EVAS_CALLBACK_MOUSE_MOVE,
	                               &on_comic_mouse_cb_move,
	                               comic);
	evas_object_event_callback_add(comic->eventsenser,
	                               EVAS_CALLBACK_MOUSE_OUT,
	                               &on_comic_mouse_cb_out,
	                               comic);

	/*  // removing this because it confusing user
	edje_object_signal_callback_add(comic->background, "mouse,clicked,1", "left" , &turn_left,  comic);
	edje_object_signal_callback_add(comic->background, "mouse,clicked,1", "right" , &turn_right, comic);
	*/
	edje_object_signal_callback_add(comic->background, "mouse,clicked,1", "list" , &show_book_menu, comic);
	edje_object_signal_callback_add(comic->background, "mouse,clicked,1", "page" , &show_page_menu, comic);
	evas_object_show(comic->eventsenser);
	evas_object_show(comic->viewport);
}

static void on_resize(Ecore_Evas *ee) {
	set_up_background();
}

int main(int args, char **argv) {
	char directory[MAX_BUF];

	if (comic_efl_init(args, argv) < 0) {
		return -1;
	}
	comic = (struct comic_reader_guis *)malloc(sizeof(struct comic_reader_guis));
	memset(comic, 0, sizeof(struct comic_reader_guis));
	comic->background = comic_edje_object_get("comic/background");
	setup_callback_functions(comic);
	set_up_background();
	ecore_evas_callback_resize_set(ee, on_resize);
	comic->config = comic_read_config_new();

	if (args >= 2) {
		sprintf(directory, "%s", argv[1]);
	} else {
		sprintf(directory, "%s/comic/", THEME_PATH);
	}

	debug("Comic book directory is: %s\n", directory);
	comic->list = comic_book_list(NULL, directory);
	debug_show_string_list(comic->list);
	if (evas_list_count(comic->list) > 0) {
		comic->book = comic_book_new((const char *)evas_list_nth(comic->list, 0));
		comic_book_goto_index(comic->book, 0);
	}



	etk_main();
	return 0;
}

